package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.AllStudentRegsiterRelations;
import com.xjc.business.entity.domain.AllStudentStayRelations;
import com.xjc.business.entity.domain.BuildingAdminRelations;
import com.xjc.business.entity.domain.StudentRepairApplys;
import com.xjc.business.entity.po.buildadmin.RepairApplyUpdatePo;
import com.xjc.business.entity.po.stu.RepairApplyPo;
import com.xjc.business.entity.vo.buildadmin.BuildRepairApplyVo;
import com.xjc.business.entity.vo.stu.RepairApplyVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.StudentRepairApplysMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【student_repair_applys(学生报修申请表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class StudentRepairApplysServiceImpl extends ServiceImpl<StudentRepairApplysMapper, StudentRepairApplys>
    implements StudentRepairApplysService {

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    AllStudentStayRelationsService allStudentStayRelationsService;

    @Autowired
    @Lazy
    BuildingAdminRelationsService buildingAdminRelationsService;

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    CollegesService collegesService;

    @Autowired
    @Lazy
    MajorsService majorsService;

    @Autowired
    @Lazy
    ClazzsService clazzsService;

    @Autowired
    @Lazy
    StudentsService studentsService;

    /**
     * 获取当前报修申请
     * @param id 学生微信id
     * @return 历史
     */
    @Override
    public Result<RepairApplyVo> getCurrentRepairApply(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();
        LambdaQueryWrapper<StudentRepairApplys> studentRepairApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentRepairApplysLambdaQueryWrapper.eq(StudentRepairApplys::getApplyId,stuId);
        studentRepairApplysLambdaQueryWrapper.in(StudentRepairApplys::getStatus,0,1);

        StudentRepairApplys one = getOne(studentRepairApplysLambdaQueryWrapper);
        if(one == null){
            return Result.okWithData(null);
        }

        RepairApplyVo repairApplyVo = new RepairApplyVo();
        repairApplyVo.setId(one.getId());
        repairApplyVo.setEquipment(one.getEquipment());
        repairApplyVo.setLocation(one.getLocation());
        repairApplyVo.setInfo(one.getInfo());
        repairApplyVo.setStatus(one.getStatus());
        repairApplyVo.setCreateTime(one.getCreateTime());

        return Result.okWithData(repairApplyVo);
    }

    /**
     * 获取历史报修申请
     * @param id 学生微信id
     * @return 历史
     */
    @Override
    public Result<List<RepairApplyVo>> getHistoryRepairApplyList(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();
        LambdaQueryWrapper<StudentRepairApplys> studentRepairApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentRepairApplysLambdaQueryWrapper.eq(StudentRepairApplys::getApplyId,stuId);
        studentRepairApplysLambdaQueryWrapper.eq(StudentRepairApplys::getStatus,2);

        List<RepairApplyVo> repairApplyVoList = list(studentRepairApplysLambdaQueryWrapper).stream().map(one -> {
            RepairApplyVo repairApplyVo = new RepairApplyVo();
            repairApplyVo.setId(one.getId());
            repairApplyVo.setEquipment(one.getEquipment());
            repairApplyVo.setLocation(one.getLocation());
            repairApplyVo.setInfo(one.getInfo());
            repairApplyVo.setStatus(one.getStatus());
            repairApplyVo.setCreateTime(one.getCreateTime());
            return repairApplyVo;
        }).collect(Collectors.toList());

        return Result.okWithData(repairApplyVoList);
    }

    /**
     * 添加报修申请
     * @param repairApplyPo 报修申请
     * @return 数据
     */
    @Override
    public Result<String> addRepairApply(RepairApplyPo repairApplyPo) {
        Integer wxId = repairApplyPo.getWxId();
        Integer stuId = wxUserService.getById(wxId).getStuId();

        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,stuId);
        AllStudentStayRelations allStudentStayRelationsFromDB = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);
        Integer buildId = allStudentStayRelationsFromDB.getBuildId();

        StudentRepairApplys studentRepairApplys = new StudentRepairApplys();
        studentRepairApplys.setApplyId(stuId);
        studentRepairApplys.setBuildId(buildId);
        studentRepairApplys.setEquipment(repairApplyPo.getEquipment());
        studentRepairApplys.setLocation(repairApplyPo.getLocation());
        studentRepairApplys.setInfo(repairApplyPo.getInfo());
        studentRepairApplys.setStatus(0);
        studentRepairApplys.setCreateTime(new Date());
        studentRepairApplys.setUpdateTime(new Date());
        save(studentRepairApplys);
        return Result.okWithData("申请成功");
    }

    /**
     * 获取报修申请列表
     * @param id 宿管Id
     * @return 数据
     */
    @Override
    public Result<List<BuildRepairApplyVo>> getRepairApplyList(Integer id) {

        LambdaQueryWrapper<BuildingAdminRelations> buildingAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingAdminRelationsLambdaQueryWrapper.eq(BuildingAdminRelations::getBuildAdminId,id);
        Integer buildId = buildingAdminRelationsService.getOne(buildingAdminRelationsLambdaQueryWrapper).getBuildId();

        LambdaQueryWrapper<StudentRepairApplys> studentRepairApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentRepairApplysLambdaQueryWrapper.eq(StudentRepairApplys::getBuildId,buildId);

        List<BuildRepairApplyVo> buildRepairApplyVoList = list(studentRepairApplysLambdaQueryWrapper).stream().map(one -> {
            BuildRepairApplyVo buildRepairApplyVo = new BuildRepairApplyVo();

            LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,one.getApplyId());
            AllStudentRegsiterRelations allStudentRegsiterRelationsFromDB = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper);
            Integer colId = allStudentRegsiterRelationsFromDB.getColId();
            Integer majId = allStudentRegsiterRelationsFromDB.getMajId();
            Integer clazzId = allStudentRegsiterRelationsFromDB.getClazzId();

            buildRepairApplyVo.setStuInfo(collegesService.getById(colId).getName()+" "+majorsService.getById(majId).getName()+"专业 "+clazzsService.getById(clazzId).getNum()+"班 "+studentsService.getById(one.getApplyId()).getName());

            buildRepairApplyVo.setId(one.getId());
            buildRepairApplyVo.setEquipment(one.getEquipment());
            buildRepairApplyVo.setLocation(one.getLocation());
            buildRepairApplyVo.setInfo(one.getInfo());
            buildRepairApplyVo.setStatus(one.getStatus());
            buildRepairApplyVo.setCreateTime(one.getCreateTime());
            return buildRepairApplyVo;
        }).collect(Collectors.toList());

        return Result.okWithData(buildRepairApplyVoList);
    }

    /**
     * 更新状态
     * @param repairApplyUpdatePo
     * @return 数据
     */
    @Override
    public Result<String> updateStatus(RepairApplyUpdatePo repairApplyUpdatePo) {
        StudentRepairApplys repairApplys = getById(repairApplyUpdatePo.getId());
        repairApplys.setStatus(repairApplyUpdatePo.getStatus());
        updateById(repairApplys);
        return Result.okWithData("更新成功");
    }
}




